﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace QD.Utils
{
    public class Parse
    {
        public HttpRequest Request { get; set; }

        public Parse(HttpRequest request)
        {
            Request = request;
        }

        private string QValue(string key)
        {
            if (Request.QueryString[key] != null)
                return Request.QueryString[key];
            else
                return Request.Form[key];
        }

        public int? Int(string key)
        {
            int val;
            return int.TryParse(QValue(key), out val) ? new Nullable<int>(val) : null;
        }

        public int Int(string key, int value)
        {
            int val;
            return int.TryParse(QValue(key), out val) ? val : value;
        }

        public float? Float(string key)
        {
            float val;
            return float.TryParse(QValue(key), out val) ? new Nullable<float>(val) : null;
        }

        public float Float(string key, float value)
        {
            float val;
            return float.TryParse(QValue(key), out val) ? val : value;
        }

        public double? Double(string key)
        {
            double val;
            return double.TryParse(QValue(key), out val) ? new Nullable<double>(val) : null;
        }

        public double Double(string key, float value)
        {
            double val;
            return double.TryParse(QValue(key), out val) ? val : value;
        }

        public long? Long(string key)
        {
            long val;
            return long.TryParse(QValue(key), out val) ? new Nullable<long>(val) : null;
        }

        public long Long(string key, long value)
        {
            long val;
            return long.TryParse(QValue(key), out val) ? val : value;
        }

        public decimal? Decimal(string key)
        {
            decimal val;
            return decimal.TryParse(QValue(key), out val) ? new Nullable<decimal>(val) : null;
        }

        public decimal Decimal(string key, decimal value)
        {
            decimal val;
            return decimal.TryParse(QValue(key), out val) ? val : value;
        }

        public short? Short(string key)
        {
            short val;
            return short.TryParse(QValue(key), out val) ? new Nullable<short>(val) : null;
        }

        public short Short(string key, short value)
        {
            short val;
            return short.TryParse(QValue(key), out val) ? val : value;
        }

        public bool? Boolean(string key)
        {
            bool val;
            bool? ret = null;
            string express = QValue(key);

            if (bool.TryParse(express, out val))
                ret = val;
            else if (express == "1")
                ret = true;
            else if (express == "0")
                ret = false;
            return ret;
        }

        public bool Boolean(string key, bool value)
        {
            bool val;
            string express = QValue(key);
            if (bool.TryParse(express, out val))
                return val;
            else if (express == "1")
                val = true;
            else if (express == "0")
                val = false;
            else
                val = value;
            return val;
        }

        public byte? Byte(string key)
        {
            byte val;
            return byte.TryParse(QValue(key), out val) ? new Nullable<byte>(val) : null;
        }

        public byte Byte(string key, byte value)
        {
            byte val;
            return byte.TryParse(QValue(key), out val) ? val : value;
        }

        public string String(string key, string value)
        {
            string val = String(key);
            return val == null ? value : val;
        }

        public string String(string key)
        {
            return QValue(key);
        }

        public DateTime? Date(string key)
        {
            DateTime val;
            return DateTime.TryParse(QValue(key), out val) ? new Nullable<DateTime>(val) : null;
        }

        public DateTime Date(string key, DateTime value)
        {
            DateTime val;
            return DateTime.TryParse(QValue(key), out val) ? val : value;
        }



        public String[] StringSplit(String key, String splitChar = ",", StringSplitOptions options = StringSplitOptions.RemoveEmptyEntries)
        {
            String val = QValue(key);
            if (System.String.IsNullOrEmpty(val)) return null;
            else return val.Split(new String[] { splitChar }, options);
        }

        public Int32[] IntSplit(String key, String splitChar = ",", StringSplitOptions options = StringSplitOptions.RemoveEmptyEntries)
        {
            String[] vals = StringSplit(key, splitChar, options);
            if (vals == null || vals.Length == 0) return null;
            Int32[] _vals = new Int32[vals.Length];
            int i = 0;
            foreach (String s in vals)
                Int32.TryParse(s, out _vals[i++]);
            return _vals;
        }

        public Int64[] LongSplit(String key, String splitChar = ",", StringSplitOptions options = StringSplitOptions.RemoveEmptyEntries)
        {
            String[] vals = StringSplit(key, splitChar, options);
            if (vals == null || vals.Length == 0) return null;
            Int64[] _vals = new Int64[vals.Length];
            int i = 0;
            foreach (String s in vals)
                Int64.TryParse(s, out _vals[i++]);
            return _vals;
        }
    }
}

